var userProfile = new UserProfile(getUserID());

function showMask(options) {
	var waitTpl = new Ext.XTemplate('<div class={cls}>{message}</div>');
	var mainPage = Ext.getCmp('mainPage');
    if (options && mainPage) {
        var maskTarget = mainPage.getEl();
        maskTarget && maskTarget.mask(waitTpl.apply(options));
    }
}

function hideMask(){
	var mainPage = Ext.getCmp('mainPage');
    if (mainPage) {
        var maskTarget = mainPage.getEl();
		if(maskTarget){
			maskTarget.unmask();
	        delete maskTarget;
		}
    }
}

function getUserID(){
	return getCookie('uid');
}

function desabledDock(bool){
	var dock = Ext.getCmp('cat_main_dock');
	dock.setDisabled(bool);
	// if(bool){
	// 	dock.hide(true);
	// }else{
	// 	dock.show(true);
	// }
}

function setActiveDockItem(idx){
	var mainPage = Ext.getCmp('cat_main_page');
	mainPage.setActiveItem(idx, 'slide');
	
}

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

Ext.setup({
    tabletStartupScreen: 'tablet_startup.png',
    phoneStartupScreen: 'phone_startup.png',
    icon: 'icon.png',
    glossOnIcon: false,
    onReady: function() {
		var catPage = new CATMainPage();
		var reportPage = new ReportPage();
		
		//Tab Panel
		var mainPage = new Ext.TabPanel({
			id: 'cat_main_page',
            tabBar: {
				id: 'cat_main_dock',
                dock: 'bottom',
                layout: {
                    pack: 'center'
                }
            },
            fullscreen: true,
            ui: 'light',
            cardSwitchAnimation: {
                type: 'slide',
                cover: true
            },

            defaults: {
                scroll: 'vertical'
            },
			listeners: {
				beforecardswitch: function(comp, newCard, oldCard, index, animated){
					if(index == 0){
						
					}else if(index == 1){
						reportPage.requestReports();
					}
				}
			},
            items: [{
                title: 'Start CAT',
				layout: 'fit',
                items:  catPage,
                iconCls: 'favorites',
				scroll: false,
                // cls: 'card1'
            }, {
                title: 'Reports',
				items: reportPage,
				layout: 'fit',
				scroll: false,
                iconCls: 'info',
                // cls: 'card2',
                // badgeText: '4'
            }, {
                title: 'Settings',
                html: 'Not Implemented',
				layout: 'fit',
                // badgeText: 'Text can go here too, but it will be cut off if it is too long.',
                // cls: 'card3',
                iconCls: 'settings'
            }, {
                title: 'About',
				layout: 'fit',
                html: 'Not Implemented',
                // cls: 'card4',
                iconCls: 'info'
            }]
        });
    }
});